package Thread;

import java.util.concurrent.CountDownLatch;

/**
 * countDownLatch位于jdk1.5并发包下
 * By LIBOFEI
 * **/
public class ThreadCountDownLatch {
    public static void main(String[] args) {
        //计数器
        final  CountDownLatch countDownLatch = new CountDownLatch(2);

        //第一个线程
        new Thread(new Runnable() {
            public void run() {
                System.out.println("我是子线程.....开始执行任务");
                try{
                    //模拟处理数据
                    Thread.sleep(20);
                }catch (Exception e){

                }
                System.out.println("我是子线程.....执行完毕");
                //执行完之后使数量-1
                countDownLatch.countDown();
            }
        }).start();

        //第二个线程
        new Thread(new Runnable() {
            public void run() {
                System.out.println("我是子线程.....开始执行任务");
                try{
                    //模拟处理数据
                    Thread.sleep(20);
                }catch (Exception e){

                }
                System.out.println("我是子线程.....执行完毕");
                //执行完之后使数量-1
                countDownLatch.countDown();
            }
        }).start();
        try{
            countDownLatch.await();
        }catch (Exception e){
        }
        //要让主线程在两个子线程执行完之后再执行
        System.out.println("我是主线程........");
    }
}
